import $service from '../../../../service.js';
var that;
const app = getApp();
let page = 1;
Page({
  data: {
    active: true,
    isShow:false,
    doctorList: [],
    isLoginshow: false,
    keyWord: "",
    historyList:[]
  },
  onLoad(e) {},
  onShow() {
    that = this.data;
    //获取聊天记录
    this.getUserSearchLogList()
  },
  // 获取医生列表
  getHomeDoctorList() {
    let params = {
      "current": 1,
      "parametn": that.keyWord,
      "size": 10
    }
    app.http.getHomeDoctorList(params, true).then(res => {
      let list = res.data.list
      if (list.length == 0) {}
      list.forEach((item, index) => {
        if (item.isOnline === 1) {
          list.splice(index, 1);
          list.unshift(item)
        }
      })
      this.setData({
        doctorList: list,
        isShow:true
      })
      this.addUserSearchLog(this.data.keyWord)
    })
  },
  //新增搜索记录
  addUserSearchLog(i){
    let params = {
      'content':i
    }
    app.http.addUserSearchLog(params).then((res)=>{
      this.getUserSearchLogList()
    })
    .catch((err)=>{
      console.log(err);
    })
  },
  //监听input框
  inputChange(e) {
    if (e.detail.value == "") {
      $service.showToast('请输入搜索内容')
    } else {
      this.setData({
        keyWord: e.detail.value
      })
      this.getHomeDoctorList()
    }
  },
  //取消搜索回到首页
  inputSearch() {
    $service.openPage('../../../user_index/user_index')
  },
  //删除所有聊天记录
  deleteHistory(){
    app.http.deleteUserSearchLog({}).then((res)=>{
      $service.showToast(res.msg)
      this.setData({
        historyList:[]
      })
    })
  },
  //获取聊天记录
  getUserSearchLogList(){
    app.http.getUserSearchLogList().then((res)=>{
      console.log(res);
      this.setData({
        historyList:res.data.list,
      })
    })
  },
  //选择记录搜索
  chooseItem(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      keyWord: this.data.historyList[index].content
    });
    this.getHomeDoctorList()
  },
  //清空输出框
  clearInput(e){
    this.setData({
      keyWord:''
    })
  }
})